# Maintainer: Jordan Irwin <antumdeluge@gmail.com>

_realname=md4c
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgbase="mingw-w64-${_realname}"
pkgver=0.4.7
pkgrel=1
pkgdesc="C Markdown parser implementation compliant to CommonMark specification"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/mity/md4c"
license=("MIT")
depends=()
makedepends=("bsdtar"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gcc")
source=(${_realname}-${pkgver}.tar.gz::"${url}/archive/release-${pkgver}.tar.gz")
sha256sums=('f1b12d7aeb64fcbc7092c832e1a8b137102fec168961c87222fa599aedc19035')

prepare() {
  cd "${srcdir}/${_realname}-release-${pkgver}"
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  mkdir -p "${srcdir}/build-${CARCH}" && cd "${srcdir}/build-${CARCH}"

  declare -a extra_config
  if check_option "debug" "n"; then
      extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
      extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}/bin/cmake" \
      -G "Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_SHARED_LIBS=ON \
      "${extra_config[@]}" \
      "../${_realname}-release-${pkgver}"

  ninja
}

package() {
  cd "${srcdir}/build-${CARCH}"
  DESTDIR="${pkgdir}" ninja install

  # license
  install -Dm644 "${srcdir}/${_realname}-release-${pkgver}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
